﻿#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <list>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Error.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetHelp_XRpc.lib")

void __stdcall XRPCCore_Callback(LPCTSTR lpszClientAddr, BOOL bStream, LPCTSTR lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE nRtnType, list<NETHELP_XRPCPARAMETE>* pStl_ListParamete, LPVOID lParam)
{
    printf("Client:%s FuncName:%s\n", lpszClientAddr, lpszFuncName);

    int nMsgLen = 1024;
    TCHAR tszMsgBuffer[1024];
    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

    if (0 == _tcsnicmp(lpszFuncName, _T("XEngine_AddMethod"), _tcslen(lpszFuncName)))
    {
        int nRet = 0;
        list<NETHELP_XRPCPARAMETE>::const_iterator stl_ListIterator = pStl_ListParamete->begin();
        for (; stl_ListIterator != pStl_ListParamete->end(); stl_ListIterator++)
        {
            int nValue = 0;
            memcpy(&nValue, stl_ListIterator->lParameteValue, stl_ListIterator->nParameteLen);

            nRet += nValue;
        }
		XRPCProtocol_Core_RepFuncCall(lpszFuncName, (LPCTSTR)&nRet, sizeof(int), nRtnType, tszMsgBuffer, &nMsgLen, bStream);
		NetHelp_XRPCCore_SendMsg(lpszClientAddr, tszMsgBuffer, nMsgLen, bStream);
    }
	if (0 == _tcsnicmp(lpszFuncName, _T("XEngine_Print"), _tcslen(lpszFuncName)))
	{
		list<NETHELP_XRPCPARAMETE>::const_iterator stl_ListIterator = pStl_ListParamete->begin();
		for (; stl_ListIterator != pStl_ListParamete->end(); stl_ListIterator++)
		{
            printf("XEngine_Print:%s\n",(LPCTSTR)stl_ListIterator->lParameteValue);
		}
		XRPCProtocol_Core_RepFuncCall(lpszFuncName, NULL, 0, nRtnType, tszMsgBuffer, &nMsgLen, bStream);
		NetHelp_XRPCCore_SendMsg(lpszClientAddr, tszMsgBuffer, nMsgLen, bStream);
    }
}
int main()
{
    WSADATA st_WSAData;
    memset(&st_WSAData, '\0', sizeof(WSADATA));
    WSAStartup(MAKEWORD(2, 2), &st_WSAData);

    LPCTSTR lpszCodeFile = _T("G:\\netengineapp\\NetEngine_WINApps\\Debug\\HttpCode.types");
    LPCTSTR lpszMiniFile = _T("G:\\netengineapp\\NetEngine_WINApps\\Debug\\HttpMime.types");

    if (!NetHelp_XRPCCore_Init(50001, 50002, lpszCodeFile, lpszMiniFile))
    {
        printf("NetHelp_XRPCCore_Init:%lX", XRpc_GetLastError());
    }
    NetHelp_XRPCCore_SetCallback(XRPCCore_Callback);

    printf("ok\n");
  
    while (1)
    {
        Sleep(5000);
    }
    NetHelp_XRPCCore_Destory();
    WSACleanup();
   
    return 0;
}